<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    //用户添加管理
    public function getAdd(){
    	//echo '11';
    	return view('admin.user.add');

    }

    //文件上传
		public function upload(Request $request,$filename){
			//dd($request->all());
			
			//检测是否有文件上传
			if($request->hasFile($filename)){
				//随机文件名
				$name = md5(time()+rand(1,9999999));
				//获取后缀名
				$su = $request->file($filename)->getClientOriginalExtension();
				$arr = array('jpg','png','jpeg','tiff');
				//判断
				if(!in_array($su,$arr)){
					echo '<script type="text/javascript">
					alert("上传文件不合法");
					location.href="http://mao.cn/test/add";
					</script>';
				}
				//dd($su);
				//将文件移动到指定目录
				$request->file($filename)->move('./uploads/'.$name.'.'.$su);
				//echo '<img src="/upload/'.$name.'.jpg" alt="">';
				return '/uploads/'.$name.'.'.$su;
			}
		}





    //执行数据插入操作
    public function postInsert(Request $request){


        //数据验证
            $this->validate($request, [
        'username' => 'required|max:255|unique:user',
        'password' => 'required',
        'email' => 'required|email',
        'phone' => 'required|regex:[1\d{10}]',
        'nc' => 'required|max:255',
    ],[
         'username.required'=>'用户名不能为空',
         'username.unique'=>'用户名已存在',
         
         'password.required'=>'密码不能为空', 
         'email.required'=>'邮箱不能为空',
         'email.email'=>'邮箱格式不正确',
         'phone.required'=>'电话不能为空',
         'phone.regex'=>'电话格式不正确',
          'nc.required'=>'昵称不能为空'
    ]);

                $data = $request->only(['username','password','email','phone','nc']);
            if($request->hasFile('pic')){
            //随机文件名
            $name = time()+rand(1,99);

             //获取后缀名
            $ext = $request->file('pic')->getClientOriginalExtension();
            // echo $name.'.'.$ext;
            //检测后缀名
            $arr = array('jpg','png','jpeg');
            //判断
            if(!in_array($ext,$arr)){
                echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="http://mao.cn/admin";
                </script>';
            }
            $request->file('pic')->move('./uploads/',$name.'.'.$ext);
            // echo './uploads/',$name.'.'.$ext;
            // dd(123);
            $data['pic'] = '/uploads/'.$name.'.'.$ext;
           
        }
        
    	
    	//提取部分数据
    	
    	
    		 $data['password'] = Hash::make($data['password']);
             
    		 // dd($data);
    	//执行数据库插入操作
    		 $res = DB::table('user')->insert($data);
    		// dd($res);
			//判断是否添加成功
	   if($res){
            return redirect('/admin/user/list')->with('success','用户添加成功');

        }else{
        	 return back()->with('error','用户添加失败');
        }
	 }



	//用户列表
	public function getList(Request $request){
		//echo '111';
		//查询所有用户数据
		//$users = DB::table('user')->get();
		
		//获取分页大小
		$num = $request->input('num',2);
		//判断是否有关键字
		if($request->input('keywords')){
			$users = DB::table('user')->where('username','like','%'.$request->input('keywords').'%')->paginate($num);
		}else{
			$users = DB::table('user')->paginate($num);
		}
		$xq = ['1'=>'管理员','2'=>'普通管理员'];
        // $xq{{$user->qx}}

        // dd($xq);
		//提取当前请求的所有参数
		$data = $request->all();


		//解析模板
		return view('admin.user.list',['user'=>$users,'request'=>$data,'xq'=>$xq]);
		
	}
		//修改表单

		 public function getEdit($id){
             //根据用户id查询数据
      		$user = DB::table('user')->where('id',$id)->first();
      			//dd($user);
      			//解析模板
      		return view('admin.user.edit',['user'=>$user]); 		
     }

     //用户修改
     	public function postUpdate(Request $request){
            $a = '';
            if($request->hasFile('pic')){
                //随机文件名
                $name = md5(time()+rand(1,9999999));
                //获取后缀名
                $su = $request->file('pic')->getClientOriginalExtension();
                $arr = array('jpg','png','jpeg','tiff');
                //判断
                if(!in_array($su,$arr)){
                    echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="http://mao.cn/admin";
                    </script>';
                }
                //将文件移动到指定目录

                $request->file('pic')->move('./uploads/',$name.'.'.$su);
                //echo '<img src="/upload/'.$name.'.jpg" alt="">';
                $a= '/uploads/'.$name.'.'.$su;

                $request->file('pic')->move('./upload/'.$name.'.'.$su);
                $a= '/upload'.$name.'.'.$su;

            }
     		//获取id
     		$id = $request->only(['id']);
     		//详细数据
     		$data = $request->only(['username','email','phone','pic','nc','qx']);
            //dd($data);
            if($a){
                 $data['pic']=$a;
            }
           
     		//执行修改
     		$res = DB::table('user')->where('id',$id)->update($data);
     		//判断是否添加成功
	   if($res){
            return redirect('/admin/user/list')->with('success','用户修改成功');

        }else{
        	 return back()->with('error','用户修改失败');
        }

    }	
    	//用户删除

     	 public  function postDelete(Request $request){

         // dd($request -> input('id'));
     	$id = $request -> input('id');

     	$res = DB::table('user')->where('id',$id)->delete();
     	if($res){
            echo 1;
     	}else{
     		echo 2;
     	}
     }


     // 用户删除
    // public  function postDelete(Request $request){
    //  	$id = $request -> input('id');
    //  	$res = DB::table('user')->where('id',$id)->delete();
    //  	if($res){
    //         echo 1;
    //  	}else{
    //  		echo 2;
    //  	}
    // }
     public function getGrxx(){
        $uid=session('id');
        if($uid){
           $users=DB::table('user')->where('id',$uid)->get();
            return view('home.zhuce.grxx',['users'=>$users]); 
        }else{
            return view('home.login.login');
        }    
     }

     // 更改用户名
     public function getUname(Request $request){
        $uid=$request->input('uid');
        $uname=$request->only('username','sheng','shi','xian');
        $uname1=$request->only('0','1','2');
        $uname['sheng']=$uname1['0'];
        $uname['shi']=$uname1['1'];
        $uname['xian']=$uname1['2'];
        $res=DB::table('user')->where('id',$uid)->update($uname);
        if($res!==null){
            return redirect('/home/xiangqing/books');
        }else{
            return redirect('/home/user/grxx');
        }
     }
     // 前台头像显示
     public function getTx(){
        $uid=session('id');
        $res=DB::table('user')->where('id',$uid)->first();
        return view('home.zhuce.tx',['user'=>$res]);
     }

     // 前台头像修改
     public function postTxxg(Request $request){
            if($request->hasFile('pic')){
            //随机文件名
            $name = time()+rand(1,99);
             //获取后缀名
            $ext = $request->file('pic')->getClientOriginalExtension();
            //检测后缀名
            $arr = array('jpg','png','jpeg');
            //判断
            if(!in_array($ext,$arr)){
                echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="http://la.cn/test/add";
                </script>';
            }
            $request->file('pic')->move('./uploads/',$name.'.'.$ext);
            $a = '/uploads/'.$name.'.'.$ext;  
        }
        // 修改头像
        $uid=session('id');
        $res=DB::table('user')->where('id',$uid)->update(['pic'=>$a]);
        if($res){
            return redirect('/home/user/grxx');
        }else{
            return redirect('/home/user/tx');
        }

     }

     // 前台密码显示更改
      // 前台密码的更改
     public function getMm(){
        return view('home.zhuce.mm');
     }
     // 前台密码的更改
     public function getMmxg(Request $request){
        $pasd=$request->only('password');
        $oldpasd=$request->input('oldpasd');
        $id=session('id');
        $old=DB::table('user')->select('password')->where('id',$id)->first();
        // 判断老密码是否一样        
        if($id){
            if(Hash::check($oldpasd, $old->password)){   
                $pasd['password'] = Hash::make($pasd['password']);
                $res=DB::table('user')->where('id',$id)->update($pasd);
                    if($res){
                        echo 1;
                    }else{
                        echo 2;
                    }
            }else{
                echo 3;
            }   
        }else{
            echo 4; 
        }
     }

    // 更改头像

    //跳转的删除页面
    public function getSc(Request $request){
        // dd($request->all());
        $uid=$request->input('id');
        return view('home.zhuce.sc',['id'=>$uid]);
    } 
    // 删除账号
    public function getUdelete(Request $request){
        // echo 222;die();
        $uid=$request->input('uid');
        // 删除用户信息
        $res1=DB::table('day')->where('uid',$uid)->delete();
        $res=DB::table('user')->where('id',$uid)->delete();
        if($res){
            return redirect('/home/index');
        }else{
            return redirect('/home/user/grxx');
        }
    } 
}
